package com.example.administrator.hqq.adapter.information;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.bean.UmengMesssage;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.example.administrator.hqq.utils.transform.GlideRoundTransform;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/12/28 19:02
 * @remark:
 */
public class NotificationListAdapter extends BaseAdapter {
    private static final int TEXT_NOTIFICATION = 0;
    private static final int GROUP_NOTIFICATION = 1;
    private List<UmengMesssage.DataBean> data;
    private Context context;
    private NotificationOnClike notificationOnClike;

    public NotificationListAdapter(Context context, List<UmengMesssage.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        switch (data.get(position).getType()) {
            case "APPLY_GROUP":
                type = 1;
                break;
        }
        return type;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        int itemViewType = getItemViewType(i);
        ViewHolder viewHolder = null;
        ViewHolder2 viewHolder2 = null;
        if (view == null) {
            switch (itemViewType) {
                case TEXT_NOTIFICATION:
                    view = LayoutInflater.from(context).inflate(R.layout.notification_list_layout, null);
                    viewHolder = new ViewHolder(view);
                    view.setTag(viewHolder);
                    break;
                case GROUP_NOTIFICATION:
                    view = LayoutInflater.from(context).inflate(R.layout.notification_list_two_item, null);
                    viewHolder2 = new ViewHolder2(view);
                    view.setTag(viewHolder2);
                    break;
            }
        } else {
            switch (itemViewType) {
                case TEXT_NOTIFICATION:
                    viewHolder = (ViewHolder) view.getTag();
                    break;
                case GROUP_NOTIFICATION:
                    viewHolder2 = (ViewHolder2) view.getTag();
                    break;
            }
        }
        switch (itemViewType) {
            case TEXT_NOTIFICATION:
                initTextNotification(viewHolder, i);
                break;
            case GROUP_NOTIFICATION:
                initGroupNotification(viewHolder2, i);
                break;
        }
        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initTextNotification(ViewHolder viewHolder, final int position) {
        switch (data.get(position).getType())
        {
            case "JUMP_GROUP":
                viewHolder.notificationIcon.setImageDrawable(context.getDrawable(R.drawable.submit_succeed));
                viewHolder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (notificationOnClike!=null){
                            notificationOnClike.jumpGroup(position);
                        }
                    }
                });
                break;
            case "JUMP_TOPIC":
                viewHolder.notificationIcon.setImageDrawable(context.getDrawable(R.drawable.topic_notification));
                viewHolder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (notificationOnClike!=null){
                            notificationOnClike.jumpTopic(position);
                        }
                    }
                });
                break;
            case "TEXT":
                viewHolder.notificationIcon.setImageDrawable(context.getDrawable(R.drawable.inform_notification));
                break;
        }
        viewHolder.title.setText(data.get(position).getTitle());
        viewHolder.text.setText(data.get(position).getText());

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initGroupNotification(ViewHolder2 viewHolder2, final int position) {
        viewHolder2.notificationIcon.setImageDrawable(context.getDrawable(R.drawable.group_chat));
        viewHolder2.title.setText(data.get(position).getTitle());
        viewHolder2.text.setText(data.get(position).getText());
        viewHolder2.questionOne.setText(data.get(position).getQuestion_1());
        viewHolder2.answerOne.setText(data.get(position).getAnswer_1());
        viewHolder2.otherAge.setText(data.get(position).getAge()+"");
        viewHolder2.otherName.setText(data.get(position).getUser_nickname());
        Glide.with(context).load(data.get(position).getUser_url())
                .bitmapTransform(new CenterCrop(context),new GlideCircleTransform(context))
                .into(viewHolder2.otherPic);
        if (data.get(position).getLocation_type()==0){
            viewHolder2.city.setVisibility(View.GONE);
        }else{
            viewHolder2.city.setVisibility(View.VISIBLE);
        }
        if (data.get(position).getGender()==1){
            viewHolder2.sex.setSelected(false);
        }else{
            viewHolder2.sex.setSelected(true);
        }
        if (TextUtils.isEmpty(data.get(position).getQuestion_2())){
           viewHolder2.q2.setVisibility(View.GONE);
        }else{
            viewHolder2.q2.setVisibility(View.VISIBLE);
            viewHolder2.questionTwo.setText(data.get(position).getQuestion_2());
            viewHolder2.answerTwo.setText(data.get(position).getAnswer_2());
        }
        switch (data.get(position).getApply_state())
        {
            case "UNCHECK":
                viewHolder2.buttonAuto.setVisibility(View.VISIBLE);
                viewHolder2.groupHint.setVisibility(View.GONE);
                viewHolder2.consent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (notificationOnClike!=null)
                        {
                            notificationOnClike.consent(position);
                        }
                    }
                });
                viewHolder2.refuse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (notificationOnClike!=null){
                            notificationOnClike.refuse(position);
                        }
                    }
                });
                break;
            case "ACCEPT":
                viewHolder2.groupHint.setVisibility(View.VISIBLE);
                viewHolder2.buttonAuto.setVisibility(View.GONE);
                viewHolder2.groupHint.setText("你已同意");
                viewHolder2.groupHint.setTextColor(Color.parseColor("#00AE9D"));
                break;
            case "DENY":
                viewHolder2.groupHint.setVisibility(View.VISIBLE);
                viewHolder2.buttonAuto.setVisibility(View.GONE);
                viewHolder2.groupHint.setText("拒绝该请求");
                viewHolder2.groupHint.setTextColor(Color.parseColor("#000000"));
                break;
        }
    }

    public void setNotificationOnClike(NotificationOnClike notificationOnClike) {
        this.notificationOnClike = notificationOnClike;
    }

    public interface NotificationOnClike {
        void consent(int position);

        void refuse(int position);

        void jumpGroup(int position);

        void jumpTopic(int position);
    }


     class ViewHolder {
        @BindView(R.id.notification_icon)
        ImageView notificationIcon;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.text)
        TextView text;
        View view;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            this.view=view;
        }
    }

    class ViewHolder2 {
        @BindView(R.id.notification_icon)
        ImageView notificationIcon;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.text)
        TextView text;
        @BindView(R.id.other_pic)
        ImageView otherPic;
        @BindView(R.id.other_name)
        TextView otherName;
        @BindView(R.id.other_age)
        TextView otherAge;
        @BindView(R.id.sex)
        ImageView sex;
        @BindView(R.id.city)
        TextView city;
        @BindView(R.id.question_one)
        TextView questionOne;
        @BindView(R.id.answer_one)
        TextView answerOne;
        @BindView(R.id.q1)
        AutoLinearLayout q1;
        @BindView(R.id.question_two)
        TextView questionTwo;
        @BindView(R.id.answer_two)
        TextView answerTwo;
        @BindView(R.id.q2)
        AutoLinearLayout q2;
        @BindView(R.id.group_hint)
        TextView groupHint;
        @BindView(R.id.consent)
        Button consent;
        @BindView(R.id.refuse)
        Button refuse;
        @BindView(R.id.button_auto)
        AutoLinearLayout buttonAuto;
        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
